package com.xlro.servlet;

import com.xlro.service.MessageBoardService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "sendMessageBoard",urlPatterns = "/sendQuest")
public class SendMessageBoard extends HttpServlet {

    private MessageBoardService messageBoardService = new MessageBoardService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        String username = (String) req.getSession().getAttribute("username");
        String type = req.getParameter("type");
        String title = req.getParameter("title");
        String desc = req.getParameter("desc");
        String phone = req.getParameter("phone");

        String typeMessage = null;
        switch (type){
            case "1":typeMessage="图书问题";break;
            case "2":typeMessage="个人用户问题";break;
            case "3":typeMessage="界面问题";break;
        }

        String message = null;
        if(typeMessage!=null)
             message = messageBoardService.add(username,typeMessage, title, desc, phone);
        if(message!=null){
            String titles = "提示";
            if("提交成功".equals(message)){
                req.getRequestDispatcher("./sendMessageBoard.jsp?title="+titles+"&username="+username+"&message="+message).forward(req,resp);
            }else {
                titles=username+typeMessage+title+desc+phone;
//            resp.getWriter().println("<script>alert('"+result+"')</script>");
                req.getRequestDispatcher("./sendMessageBoard.jsp?title="+titles+"&message="+message).forward(req,resp);
//            resp.getWriter().println("<script src='layui/layui.js'></script> <script>layui.use('layer', function(){layui.layerl.msg('hello')</script>"  );
            }
        }

    }
}
